package otherapi.itext.itextpdf;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.lowagie.text.Cell;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author LiDong
 * @version 1.0.0
 * @createTime 4/16/2022 10:39 AM
 */
public class ItextPdfTest {

    private static final Logger logger = LoggerFactory.getLogger(ItextPdfTest.class);

    private static final float[] columnWidths5 = {0.5f, 0.5f, 0.5f, 0.5f, 0.5f};

    /**
     * 生成 ItextPdf
     *
     * @return String
     */
    private String createItextPdf() {
        Date date = new Date();
        String startTime = new SimpleDateFormat("yyyyMMddHHmmss").format(date);
        String fileName = "demo1";
        String oldPath = fileName + ".pdf";
        // 创建文件
        Document document = new Document();
        PdfWriter writer = null;
        try {
            document.setPageSize(PageSize.A4);
            // 建立一个书写器
            writer = PdfWriter.getInstance(document, new FileOutputStream(oldPath));
            ItextPdfUtils.setFooter(writer, "测试页眉", 12, PageSize.LEGAL);
            // 打开文件
            document.open();

            // 总表设计一个 4 列的表.
            PdfPTable table = new PdfPTable(4);
            table.setWidthPercentage(100);
            table.setSpacingBefore(100f);
            table.setSpacingAfter(10f);

            // 设置列宽
            float[] columnWidths = {0.8f, 0.8f, 0.5f, 0.5f};
            table.setWidths(columnWidths);

            PdfPCell cell;
            // 第一行
            table.addCell(ItextPdfUtils.getCell("", null, ItextPdfUtils.TWENTY, true, Cell.ALIGN_CENTER, Cell.ALIGN_MIDDLE, 0, null, 0, 0, 0, 0));
            table.addCell(ItextPdfUtils.getCell("ItextPdf 打印测试", ItextPdfUtils.smallThreeFont, ItextPdfUtils.TWENTY, true, Cell.ALIGN_CENTER, Cell.ALIGN_MIDDLE, 0, null, 0, 0, 0, 0));
            table.addCell(ItextPdfUtils.getCell("", ItextPdfUtils.fiveFont, ItextPdfUtils.TWENTY, true, Cell.ALIGN_RIGHT, Cell.ALIGN_BOTTOM, 2, null, 0, null, null, null));
            // 第三行
            table.addCell(ItextPdfUtils.getCell("记录人：张三", ItextPdfUtils.fiveFont, ItextPdfUtils.TWENTY, true, Cell.ALIGN_LEFT, Cell.ALIGN_MIDDLE, 0, null, 0, null, null, null));
            table.addCell(ItextPdfUtils.getCell("打印日期：" + new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(new Date()), ItextPdfUtils.fiveFont, ItextPdfUtils.TWENTY, true, Cell.ALIGN_CENTER, Cell.ALIGN_MIDDLE, 0, null, 0, null, null, null));
            table.addCell(ItextPdfUtils.getCell("编号：AAAAAAAAAAAAA号", ItextPdfUtils.fiveFont, ItextPdfUtils.TWENTY, true, Cell.ALIGN_RIGHT, Cell.ALIGN_MIDDLE, 2, null, 0, null, null, null));

            // 第一个表格
            PdfPTable personInfoTable = new PdfPTable(5);
            float[] headerTableColumnWidth = {0.5f, 0.5f, 0.5f, 0.5f, 0.5f};
            personInfoTable.setWidthPercentage(100);
            personInfoTable.setWidths(headerTableColumnWidth);
            personInfoTable.addCell(ItextPdfUtils.getCell("编号", ItextPdfUtils.fiveFont, ItextPdfUtils.Thirty, Cell.ALIGN_MIDDLE));
            personInfoTable.addCell(ItextPdfUtils.getCell("", ItextPdfUtils.fiveFont, ItextPdfUtils.Thirty, Cell.ALIGN_MIDDLE));
            personInfoTable.addCell(ItextPdfUtils.getCell("姓名", ItextPdfUtils.fiveFont, ItextPdfUtils.Thirty, Cell.ALIGN_MIDDLE));
            personInfoTable.addCell(ItextPdfUtils.getCell("", ItextPdfUtils.fiveFont, ItextPdfUtils.Thirty, Cell.ALIGN_MIDDLE));

            Image img = Image.getInstance("demo.jpg");
            PdfPCell imageCell = new PdfPCell(new Phrase("照片", ItextPdfUtils.fiveFont));
            imageCell.setVerticalAlignment(Cell.ALIGN_MIDDLE);
            imageCell.setHorizontalAlignment(Cell.ALIGN_CENTER);
            imageCell.setRowspan(4);
            imageCell.setImage(img);
            personInfoTable.addCell(imageCell);
            personInfoTable.addCell(ItextPdfUtils.getCell("性别", ItextPdfUtils.fiveFont, ItextPdfUtils.Thirty, Cell.ALIGN_MIDDLE));
            personInfoTable.addCell(ItextPdfUtils.getCell("", ItextPdfUtils.fiveFont, ItextPdfUtils.Thirty, Cell.ALIGN_MIDDLE));
            personInfoTable.addCell(ItextPdfUtils.getCell("籍贯", ItextPdfUtils.fiveFont, ItextPdfUtils.Thirty, Cell.ALIGN_MIDDLE));
            personInfoTable.addCell(ItextPdfUtils.getCell("", ItextPdfUtils.fiveFont, ItextPdfUtils.Thirty, Cell.ALIGN_MIDDLE));
            personInfoTable.addCell(ItextPdfUtils.getCell("民族", ItextPdfUtils.fiveFont, ItextPdfUtils.Thirty, Cell.ALIGN_MIDDLE));
            personInfoTable.addCell(ItextPdfUtils.getCell("", ItextPdfUtils.fiveFont, ItextPdfUtils.Thirty, Cell.ALIGN_MIDDLE));
            personInfoTable.addCell(ItextPdfUtils.getCell("出生日期", ItextPdfUtils.fiveFont, ItextPdfUtils.Thirty, Cell.ALIGN_MIDDLE));
            personInfoTable.addCell(ItextPdfUtils.getCell("", ItextPdfUtils.fiveFont, ItextPdfUtils.Thirty, Cell.ALIGN_MIDDLE));
            personInfoTable.addCell(ItextPdfUtils.getCell("政治面貌", ItextPdfUtils.fiveFont, ItextPdfUtils.Thirty, Cell.ALIGN_MIDDLE));
            personInfoTable.addCell(ItextPdfUtils.getCell("", ItextPdfUtils.fiveFont, ItextPdfUtils.Thirty, Cell.ALIGN_MIDDLE));
            personInfoTable.addCell(ItextPdfUtils.getCell("学历", ItextPdfUtils.fiveFont, ItextPdfUtils.Thirty, Cell.ALIGN_MIDDLE));
            personInfoTable.addCell(ItextPdfUtils.getCell("", ItextPdfUtils.fiveFont, ItextPdfUtils.Thirty, Cell.ALIGN_MIDDLE));
            PdfPCell personInfoCell = new PdfPCell(new Phrase(""));
            personInfoCell.setColspan(4);
            personInfoCell.addElement(personInfoTable);
            // 表格的总行高
            personInfoCell.setFixedHeight(120);
            table.addCell(personInfoCell);

            // 备注说明
            PdfPTable remarkTable = new PdfPTable(2);
            remarkTable.setWidthPercentage(100);
            float[] remarkColumnWidth = {0.1f, 0.9f};
            remarkTable.setWidths(remarkColumnWidth);
            PdfPCell remarkCell;
            remarkCell = new PdfPCell(new Phrase("情况说明（个人家庭情况做简单说明）：", ItextPdfUtils.fiveFont));
            remarkCell.setHorizontalAlignment(Cell.ALIGN_CENTER);
            remarkCell.setVerticalAlignment(Cell.ALIGN_MIDDLE);
            remarkTable.addCell(remarkCell);
            remarkCell = new PdfPCell(new Phrase("", ItextPdfUtils.fiveFont));
            remarkCell.setMinimumHeight(200);
            remarkTable.addCell(remarkCell);

            PdfPCell remark = new PdfPCell(new Phrase(""));
            remark.setMinimumHeight(200);
            remark.setColspan(4);
            remark.addElement(remarkTable);
            table.addCell(remark);

            // 姓名性别表格
            PdfPTable mainLeftTable = new PdfPTable(2);
            mainLeftTable.setWidthPercentage(100);
            float[] columnWidths0 = {0.8f, 0.8f};
            mainLeftTable.setWidths(columnWidths0);

            mainLeftTable.addCell(ItextPdfUtils.getCell("家庭成员", ItextPdfUtils.fiveFont, 28, true, Cell.ALIGN_CENTER, Cell.ALIGN_MIDDLE));
            mainLeftTable.addCell(ItextPdfUtils.getCell("身份证号", ItextPdfUtils.fiveFont, 28, true, Cell.ALIGN_CENTER, Cell.ALIGN_MIDDLE));
            mainLeftTable.addCell(ItextPdfUtils.getCell("李四", ItextPdfUtils.fiveFont, ItextPdfUtils.TWENTY, true, Cell.ALIGN_CENTER, Cell.ALIGN_MIDDLE));
            mainLeftTable.addCell(ItextPdfUtils.getCell("632123111100000000", ItextPdfUtils.fiveFont, ItextPdfUtils.TWENTY, true, Cell.ALIGN_CENTER, Cell.ALIGN_MIDDLE));
            mainLeftTable.addCell(ItextPdfUtils.getCell("王五", ItextPdfUtils.fiveFont, ItextPdfUtils.TWENTY, true, Cell.ALIGN_CENTER, Cell.ALIGN_MIDDLE));
            mainLeftTable.addCell(ItextPdfUtils.getCell("632123111100000000", ItextPdfUtils.fiveFont, ItextPdfUtils.TWENTY, true, Cell.ALIGN_CENTER, Cell.ALIGN_MIDDLE));
            mainLeftTable.addCell(ItextPdfUtils.getCell("王五", ItextPdfUtils.fiveFont, ItextPdfUtils.TWENTY, true, Cell.ALIGN_CENTER, Cell.ALIGN_MIDDLE));
            mainLeftTable.addCell(ItextPdfUtils.getCell("632123111100000000", ItextPdfUtils.fiveFont, ItextPdfUtils.TWENTY, true, Cell.ALIGN_CENTER, Cell.ALIGN_MIDDLE));

            //左上角
            cell = new PdfPCell(new Phrase("", ItextPdfUtils.fiveFont));
            cell.setMinimumHeight(80);
            cell.setUseAscender(true);
            cell.setHorizontalAlignment(Cell.ALIGN_CENTER);
            cell.setVerticalAlignment(Cell.ALIGN_MIDDLE);
            cell.setRowspan(5);
            cell.setColspan(2);
            cell.setBorderWidthRight(0);
            cell.setBorderWidthBottom(0);
            cell.setPaddingRight(-0.2f);
            cell.addElement(mainLeftTable);
            table.addCell(cell);

            // 设计一个3列的表.
            PdfPTable rightOne = new PdfPTable(3);
            // 宽度100%填充
            rightOne.setWidthPercentage(100);
            float[] columnWidths1 = {0.06f, 0.06f, 0.06f};
            rightOne.setWidths(columnWidths1);

            rightOne.addCell(ItextPdfUtils.getCell("正常收入", ItextPdfUtils.fiveFont, 14, true, Cell.ALIGN_CENTER, Cell.ALIGN_TOP, 3));
            rightOne.addCell(ItextPdfUtils.getCell("千", ItextPdfUtils.fiveFont, 14, true, Cell.ALIGN_CENTER, Cell.ALIGN_TOP));
            rightOne.addCell(ItextPdfUtils.getCell("百", ItextPdfUtils.fiveFont, 14, true, Cell.ALIGN_CENTER, Cell.ALIGN_TOP));
            rightOne.addCell(ItextPdfUtils.getCell("十", ItextPdfUtils.fiveFont, 14, true, Cell.ALIGN_CENTER, Cell.ALIGN_TOP));
            // 正常收入  3行
            for (int i = 0; i < 9; i++) {
                rightOne.addCell(ItextPdfUtils.getCell("", ItextPdfUtils.fiveFont, ItextPdfUtils.TWENTY, true, Cell.ALIGN_CENTER, Cell.ALIGN_TOP));
            }
            //正常收入 3行
            cell = new PdfPCell(new Phrase("", ItextPdfUtils.fiveFont));
            cell.setMinimumHeight(68);
            cell.setUseAscender(true);
            cell.setHorizontalAlignment(Cell.ALIGN_CENTER);
            cell.setVerticalAlignment(Cell.ALIGN_TOP);
            cell.setBorderWidthRight(0);
            cell.setBorderWidthLeft(0);
            cell.setBorderWidthBottom(0);
            cell.setPaddingLeft(2.5f);
            cell.setPaddingBottom(-3);
            cell.setRowspan(5);
            cell.addElement(rightOne);
            table.addCell(cell);

            // 设计一个 3 列的表.
            PdfPTable rightTwo = new PdfPTable(3);
            // 宽度100%填充
            rightTwo.setWidthPercentage(102);
            // 设置列宽
            float[] columnWidths2 = {0.06f, 0.06f, 0.06f};
            rightTwo.setWidths(columnWidths2);
            PdfPCell cell2;
            rightTwo.addCell(ItextPdfUtils.getCell("补助", ItextPdfUtils.fiveFont, 14, true, Cell.ALIGN_CENTER, Cell.ALIGN_TOP, 3));
            rightTwo.addCell(ItextPdfUtils.getCell("千", ItextPdfUtils.fiveFont, 14, true, Cell.ALIGN_CENTER, Cell.ALIGN_TOP));
            rightTwo.addCell(ItextPdfUtils.getCell("百", ItextPdfUtils.fiveFont, 14, true, Cell.ALIGN_CENTER, Cell.ALIGN_TOP));
            rightTwo.addCell(ItextPdfUtils.getCell("十", ItextPdfUtils.fiveFont, 14, true, Cell.ALIGN_CENTER, Cell.ALIGN_TOP));
            // 补助 3 行
            for (int i = 0; i < 9; i++) {
                rightTwo.addCell(ItextPdfUtils.getCell("十", ItextPdfUtils.fiveFont, ItextPdfUtils.TWENTY, true, Cell.ALIGN_CENTER, Cell.ALIGN_TOP));

            }
            // 补助 3行
            cell = new PdfPCell(new Phrase("", ItextPdfUtils.fiveFont));
            cell.setMinimumHeight(80);
            cell.setUseAscender(true);
            cell.setHorizontalAlignment(Cell.ALIGN_CENTER);
            cell.setVerticalAlignment(Cell.ALIGN_TOP);
            cell.setBorderWidthLeft(0);
            cell.setBorderWidthBottom(0);
            cell.setPaddingRight(3);
            cell.setPaddingBottom(-3);
            cell.setRowspan(5);
            cell.addElement(rightTwo);
            table.addCell(cell);

            // 设计一个1列的表.
            PdfPTable table4 = new PdfPTable(1);
            table4.setWidthPercentage(100);
            float[] columnWidths4 = {1.6f};
            table4.setWidths(columnWidths4);
            table4.addCell(ItextPdfUtils.getCell("金额合计：捌佰陆拾壹元陆角玖分", ItextPdfUtils.fiveFont, ItextPdfUtils.TWENTY, true, Cell.ALIGN_LEFT, Cell.ALIGN_MIDDLE));

            //最后一行
            cell = new PdfPCell(new Phrase("", ItextPdfUtils.fiveFont));
            cell.setBorderWidthRight(0);
            cell.setBorderWidthTop(0);
            cell.setPaddingRight(-0.2f);
            cell.setPaddingTop(-0.5f);
            cell.addElement(table4);
            cell.setColspan(2);
            table.addCell(cell);

            // 设计一个 3 列的表.
            PdfPTable table3 = new PdfPTable(3);
            table3.setWidthPercentage(102);
            float[] columnWidths3 = {0.06f, 0.06f, 0.06f,};
            table3.setWidths(columnWidths3);
            for (int i = 0; i < 3; i++) {
                table3.addCell(ItextPdfUtils.getCell("", ItextPdfUtils.smallSixFont, ItextPdfUtils.TWENTY, true, Cell.ALIGN_CENTER, Cell.ALIGN_MIDDLE));
            }
            cell = new PdfPCell(new Phrase("", ItextPdfUtils.smallFiveFont));
            cell.setBorderWidthRight(0);
            cell.setBorderWidthLeft(0);
            cell.setBorderWidthTop(0);
            cell.setPaddingLeft(2.5f);
            cell.setPaddingTop(-0.5f);
            cell.addElement(table3);
            table.addCell(cell);

            cell = new PdfPCell(new Phrase("", ItextPdfUtils.smallFiveFont));
            cell.setBorderWidthLeft(0);
            cell.setBorderWidthTop(0);
            cell.setPaddingTop(-0.5f);
            cell.setPaddingRight(3);
            cell.addElement(table3);
            table.addCell(cell);


            // 领导意见
            PdfPTable remarkTableOne = new PdfPTable(2);
            remarkTableOne.setWidthPercentage(100);
            remarkTableOne.setWidths(remarkColumnWidth);
            remarkTableOne.addCell(ItextPdfUtils.getCell("领导意见", ItextPdfUtils.fiveFont, 200, true, Cell.ALIGN_CENTER, Cell.ALIGN_CENTER));
            remarkTableOne.addCell(ItextPdfUtils.getCell("", ItextPdfUtils.fiveFont, 200, true, Cell.ALIGN_CENTER, Cell.ALIGN_CENTER));
            remarkTableOne.addCell(ItextPdfUtils.getCell("", ItextPdfUtils.fiveFont, 200));
            PdfPCell remarkOne = new PdfPCell(new Phrase(""));
            remarkOne.setMinimumHeight(200);
            remarkOne.setColspan(4);
            remarkOne.addElement(remarkTableOne);
            table.addCell(remarkOne);


            document.add(table);

            // 表尾
            Paragraph paragraphTail = new Paragraph("制单人：李某某", ItextPdfUtils.fiveFont);
            paragraphTail.setLeading(10);
            paragraphTail.setAlignment(Element.ALIGN_LEFT);
            document.add(paragraphTail);

            // 第二页
            createTwoPage(document);
        } catch (Exception e) {
            logger.error("Error Occur:", e);
        } finally {
            // 关闭文档
            document.close();
            // 关闭书写器
            if (writer != null) {
                writer.close();
            }
        }
        return oldPath;
    }

    /**
     * 第二三页的内容
     *
     * @param document
     * @throws DocumentException
     */
    private void createTwoPage(Document document) throws DocumentException {
        document.newPage();
        document.setMargins(4, 4, 4, 50);
        document.addAuthor("张三");
        document.addHeader("测试", "测试打印页头部");
        PdfPTable table = new PdfPTable(5);
        table.setWidths(columnWidths5);
        table.setWidthPercentage(100);
        PdfPCell cell;
        for (int i = 0; i < 500; i++) {
            if (i <= 4) {
                cell = new PdfPCell(new Phrase("测试列" + i, ItextPdfUtils.fiveFont));
                cell.setMinimumHeight(20);
            } else {
                cell = new PdfPCell(new Phrase(String.valueOf(i), ItextPdfUtils.fiveFont));
                cell.setMinimumHeight(15);
            }
            table.addCell(cell);
        }
        document.add(table);
    }

    @Test
    public void test1() {
        createItextPdf();
    }
}
